package com.henu.xyh;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.henu.R;
import com.henu.yizhiya.entity.Person;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class SexActivity extends AppCompatActivity {
    private ImageButton sex_return;
    private Button sex_save;
    private RadioGroup radioGroup;
    String sex=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sex);
        sex_return= (ImageButton) findViewById(R.id.sex_return);
        sex_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId==R.id.rb_male){
                    sex="男";
                }else{
                    sex="女";
                }
            }
        });

        sex_save= (Button) findViewById(R.id.sex_save);
        sex_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Person person = BmobUser.getCurrentUser(Person.class);
                person.setSex(sex);
                person.update(person.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if(e==null){
                            Toast.makeText(SexActivity.this, "成功", Toast.LENGTH_SHORT).show();
                            Person p = BmobUser.getCurrentUser(Person.class);
                            Intent intent = new Intent(SexActivity.this, MyinformationActivity.class);
                            startActivity(intent);
                            finish();
                        }else{
                            Toast.makeText(SexActivity.this, "失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

}
